#!/bin/bash

if [ ""$1"" = "" ];then
	targetPlatform="Darwin"
else
	targetPlatform=$1
fi

if [ ""$2"" = "" ];then
	targetArchitecture="i386"
else
	targetArchitecture=$2
fi

SCONS=`which scons`
if [ -z "$SCONS" ]; then
echo "Please install scons"
exit 1
fi
mkdir -p /Developer/CocoScriptKit/1.0/Download/v8
svn checkout http://v8.googlecode.com/svn/trunk/ /Developer/CocoScriptKit/1.0/Download/v8
mkdir -p /Developer/CocoScriptKit/1.0/build/$targetPlatform/$targetArchitecture
cp -rf /Developer/CocoScriptKit/1.0/Download/v8 /Developer/CocoScriptKit/1.0/build/$targetPlatform/$targetArchitecture
cd /Developer/CocoScriptKit/1.0/build/$targetPlatform/$targetArchitecture/v8
if [ $targetPlatform = "Windows" ];then
	sed "s/mmacosx-version-min=10.4/DWIN32', '-std=c++0x/g" SConstruct > SConstruct.tmp
	mv SConstruct.tmp SConstruct
	cd src
	sed "s/'platform-macos.cc', 'platform-posix.cc'/'platform-win32.cc', 'win32-math.cc'/g" SConscript > SConscript.tmp
        mv SConscript.tmp SConscript

        sed "s/#include <wspiapi.h>//g" win32-headers.h > win32-headers.h.tmp
	mv win32-headers.h.tmp win32-headers.h
        sed "s/_tzset();//g"  platform-win32.cc > platform-win32.cc.tmp
        mv platform-win32.cc.tmp platform-win32.cc

	cd ..
        export CXX=/Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-g++
fi
scons

mkdir  -p /Developer/CocoScriptKit/1.0/$targetPlatform/$targetArchitecture/v8/lib
mkdir  -p /Developer/CocoScriptKit/1.0/$targetPlatform/$targetArchitecture/v8/include

cp /Developer/CocoScriptKit/1.0/build/$targetPlatform/$targetArchitecture/v8/*.a  /Developer/CocoScriptKit/1.0/$targetPlatform/$targetArchitecture/v8/lib

cp /Developer/CocoScriptKit/1.0/build/$targetPlatform/$targetArchitecture/v8/include/*  /Developer/CocoScriptKit/1.0/$targetPlatform/$targetArchitecture/v8/include

